package boundary;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Toolkit;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import dataview.Sostanza;
import dataview.Unita;
import dataview.Valore;
import dataview.Valori;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.WindowConstants;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import java.awt.Insets;
import java.awt.Cursor;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JLabel;

public class InserisciValori extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private ModificaInfoCibi main;
	
	private Valori valori;
	private Valori nValori;
	private Valori valoriEliminati;
	private JPanel jContentPane = null;
	private JTextField jTextField = null;
	private JComboBox jComboBox = null;
	private JComboBox jComboBox1 = null;
	private JList jList = null;
	private DefaultListModel model;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JScrollPane jScrollPane = null;
	private JPanel insPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JPanel buttonBar = null;
	
	/**
	 * @param owner
	 */
	public InserisciValori(Frame owner, ModificaInfoCibi main, Valori valori) {
		super(owner);
		this.main = main;
		this.valori = valori;
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(528, 208);
		this.setPreferredSize(new Dimension(528, 246));
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (d.width - 528) / 2;
		int y = (d.height - 246) / 2;
		this.setLocation(x, y);
		this.setLocationRelativeTo(this.getOwner());
		this.setTitle("Inserisci valori");
		this.setResizable(false);
		this.setModal(true);
		this.setContentPane(getJContentPane());
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridwidth = 6;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridwidth = 2;
			gridBagConstraints2.gridx = -1;
			gridBagConstraints2.gridy = -1;
			gridBagConstraints2.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints81 = new GridBagConstraints();
			gridBagConstraints81.gridx = 0;
			gridBagConstraints81.gridheight = 1;
			gridBagConstraints81.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints81.anchor = GridBagConstraints.NORTH;
			gridBagConstraints81.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints81.gridy = 1;
			GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
			gridBagConstraints71.fill = GridBagConstraints.BOTH;
			gridBagConstraints71.gridy = 1;
			gridBagConstraints71.weightx = 1.0;
			gridBagConstraints71.weighty = 1.0;
			gridBagConstraints71.gridwidth = 5;
			gridBagConstraints71.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints71.gridx = 1;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			jContentPane.add(getJScrollPane(), gridBagConstraints71);
			jContentPane.add(getInsPanel(), gridBagConstraints81);
			jContentPane.add(getButtonBar(), gridBagConstraints4);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("0");
			jTextField.setFont(new Font("Dialog", Font.PLAIN, 12));
			jTextField.setColumns(4);
			jTextField.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					jTextField.setText("");
				}
			});
			jTextField.addKeyListener(new NumericAdapter());
		}
		return jTextField;
	}
	
	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			Set<String> unita = new TreeSet<String>();
			for (Unita u : Unita.values())
				unita.add(u.toString());
			unita.add("Seleziona unita..");
			jComboBox = new JComboBox(unita.toArray());
			jComboBox.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return jComboBox;
	}
	
	/**
	 * This method initializes jComboBox1
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			Set<String> sostanze = new TreeSet<String>();
			for (Sostanza s : Sostanza.values())
				sostanze.add(s.toString().replace("_", " "));
			sostanze.add("Seleziona sostanza..");
			jComboBox1 = new JComboBox(sostanze.toArray());
			jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return jComboBox1;
	}
	
	/**
	 * This method initializes jList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJList() {
		if (jList == null) {
			model = new DefaultListModel();
			if (valori != null) for (Valore v : valori)
				model.addElement(v);
			jList = new JList(model);
			jList.setFont(new Font("Dialog", Font.PLAIN, 12));
			jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
				public void valueChanged(javax.swing.event.ListSelectionEvent e) {
					jButton3.setEnabled(getJList().getSelectedIndex() != -1);
				}
			});
		}
		return jList;
	}
	
	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Aggiungi");
			jButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton.setHorizontalTextPosition(SwingConstants.TRAILING);
			jButton.setIcon(new ImageIcon(getClass().getResource("/icone/add-icon.png")));
			jButton.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton.addActionListener(this);
		}
		return jButton;
	}
	
	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Annulla");
			jButton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton1.setHorizontalTextPosition(SwingConstants.TRAILING);
			jButton1.setIcon(new ImageIcon(getClass().getResource("/icone/chart-remove-icon.png")));
			jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton1.addActionListener(this);
		}
		return jButton1;
	}
	
	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Conferma");
			jButton2.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton2.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton2.setHorizontalTextPosition(SwingConstants.TRAILING);
			jButton2.setIcon(new ImageIcon(getClass().getResource("/icone/chart-accept-icon.png")));
			jButton2.setEnabled(valori == null ? false : true);
			jButton2.addActionListener(this);
		}
		return jButton2;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButton) { // tasto aggiungi
			String quant = jTextField.getText();
			String unita = (String)jComboBox.getSelectedItem();
			String sost = (String)jComboBox1.getSelectedItem();
			do {
				if (jComboBox1.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(this, "Inserisci prima una sostanza", "Errore", JOptionPane.WARNING_MESSAGE);
					break;
				}
				if (quant.length() < 1 || Integer.parseInt(quant) < 1) {
					JOptionPane.showMessageDialog(this, "Inserisci un numero maggiore di zero", "Errore", JOptionPane.WARNING_MESSAGE);
					break;
				}
				if (jComboBox.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(this, "Seleziona prima una unita'", "Errore", JOptionPane.WARNING_MESSAGE);
					break;
				}
				if (valori == null) valori = new Valori();
				Valore valore = new Valore(Sostanza.valueOf(sost.replace(" ", "_")), Integer.parseInt(quant), Unita.valueOf(unita));
				if (nValori == null) nValori = new Valori();
				nValori.add(valore);
				model.addElement(valore);
				jList.repaint();
			} while (false);
			if (nValori != null && nValori.size() > 0) jButton2.setEnabled(true);
			/*
			 * TODO: quando aggiungo il modo di eliminare dalla lista ricordarsi di verificare se la
			 * lista non contiene elementi in quel caso jbutton2.setEnabled(false)
			 */
		} else if (e.getSource() == jButton1) {
			dispose();
		} else if (e.getSource() == jButton2) {
			if (valori != null && nValori != null) for (Valore v : nValori)
				valori.add(v);
			if (valori != null && valoriEliminati != null) for (Valore v : valoriEliminati)
				valori.remove(v);
			
			main.passaValori(valori);
			dispose();
		}
	}
	
	/**
	 * This method initializes jButton3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("Rimuovi");
			jButton3.setEnabled(false);
			jButton3.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton3.setHorizontalTextPosition(SwingConstants.TRAILING);
			jButton3.setIcon(new ImageIcon(getClass().getResource("/icone/meno-icon.png")));
			jButton3.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int sel = getJList().getSelectedIndex();
					getJList().setSelectedIndex(-1);
					if (valoriEliminati == null) valoriEliminati = new Valori();
					valoriEliminati.add((Valore)((DefaultListModel)getJList().getModel()).remove(sel));
					getJList().setVisible(true);
				}
			});
		}
		return jButton3;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}
	
	/**
	 * This method initializes insPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getInsPanel() {
		if (insPanel == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridy = 2;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Unita'");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 3);
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Quantita'");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Sostanza");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.gridx = 1;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			insPanel = new JPanel();
			insPanel.setLayout(new GridBagLayout());
			insPanel.setBorder(BorderFactory.createTitledBorder(null, "Inserimento valori per " + main.getUnitaBox().getSelectedItem(), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			insPanel.add(getJTextField(), gridBagConstraints);
			insPanel.add(getJComboBox1(), gridBagConstraints3);
			insPanel.add(jLabel, gridBagConstraints5);
			insPanel.add(jLabel1, gridBagConstraints6);
			insPanel.add(jLabel2, gridBagConstraints7);
			insPanel.add(getJComboBox(), gridBagConstraints9);
		}
		return insPanel;
	}
	
	/**
	 * This method initializes buttonBar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonBar() {
		if (buttonBar == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridy = 0;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.anchor = GridBagConstraints.WEST;
			gridBagConstraints10.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints10.gridx = 3;
			gridBagConstraints10.gridy = -1;
			gridBagConstraints10.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridx = 2;
			gridBagConstraints8.gridy = -1;
			gridBagConstraints8.insets = new Insets(5, 30, 5, 5);
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST;
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = -1;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			buttonBar = new JPanel();
			buttonBar.setLayout(new GridBagLayout());
			buttonBar.add(getJButton3(), gridBagConstraints1);
			buttonBar.add(getJButton1(), gridBagConstraints8);
			buttonBar.add(getJButton2(), gridBagConstraints10);
			buttonBar.add(getJButton(), gridBagConstraints11);
		}
		return buttonBar;
	}
	
} //  @jve:decl-index=0:visual-constraint="10,60"
